#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;

int m;
int n;
int I[102];
int R[102];
unordered_set<int> myset;
int cur;
int out[408];

bool hasNumber(int n, int number) {
    string txt = to_string(n);
    string key = to_string(number);
    if (txt.find(key) == string::npos) return 0;
    return 1;
}

int main() {
    cin >> m;
    for (int i = 0; i < m; i++) cin >> I[i];
    cin >> n;
    int v;
    for (int i = 0; i < n; i++) {
        cin >> v;
        myset.insert(v);
    }
    n = myset.size();
    int Rcur = 0;
    for (auto it = myset.begin(); it != myset.end(); it++) R[Rcur++] = *it;
    sort(R, R + Rcur);

    cur = 1;
    for (int i = 0; i < n; i++) {
        int cnt = 0;
        for (int j = 0; j < m; j++) {
            if (hasNumber(I[j], R[i])) {
                cnt += 1;
            }
        }
        if (cnt != 0) {
            // cout << "cnt: " << cnt << endl;
            out[cur++] = R[i];
            out[cur++] = cnt;
            for (int j = 0; j < m; j++) {
                if (hasNumber(I[j], R[i])) {
                    out[cur++] = j;
                    out[cur++] = I[j];
                }
            }
        }
    }

    out[0] = cur - 1;
    for (int i = 0; i < cur; i++) cout << out[i] << " ";

    return 0;
}